package single_game.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import main.CyclingManager;
import single_game.logic.race_simulator.Race;
import single_game.logic.race_simulator.RaceSimulator;
import single_game.logic.race_simulator.race_team.RaceRider;
import data.Cyclist;

public class RaceGui implements MouseListener {
	private GridBagLayout layout, 
						  mainLayout,
						  userLayout;
	private Font font = new Font("Times New Roman", 1, 13), font2 = new Font("Times New Roman", 1, 20);
	private JLabel[] stats;
	private JLabel[][] riders;
	private JLabel name;
	private String[] attributeNames = { "LP", "Surname", "Team", "Time", "Puls", "Stamina", "Energy" };
	private int attributesNum = 7;
	private ArrayList<Cyclist> cyclists;
	private JFrame frame;
	private JPanel standingPanel,
	 			   userPanel;
	private RiderActions riderActions;				  
	private JScrollPane standingPane;
	public ArrayList<RaceRider> myTeam;
	private RiderPanel riderPanel[];
	private int riderPanelHeight = 70,
				riderPanelWidth = 200,
				standingsPanelHeight = 400,
				standingsPanelWidth = 520;
	public int lastSelected = -1;
	public ProfileGUI profileGui = null;
	private DecimalFormat df = new DecimalFormat("#.##");
	
	public RaceGui(JFrame frame, int cyclistNum, ArrayList<RaceRider> myTeam) {
		this.myTeam = myTeam;
		this.frame = frame;
		cyclists = CyclingManager.getInstance().cyclists;
		frame.getContentPane().removeAll();
		frame.getContentPane().revalidate();	
		frame.getContentPane().repaint();		
		frame.setTitle("Cycling Manager - Race");	
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setBackground(new Color(0, 180, 255));
		
		mainLayout = new GridBagLayout();
		
		GridBagConstraints cb = new GridBagConstraints();
		cb.fill = GridBagConstraints.BOTH;
		cb.insets = new Insets(0,0,2,0);
		
		mainLayout.columnWidths = new int[] { riderPanelWidth + 10, 590, standingsPanelWidth };
		mainLayout.rowHeights = new int[] {standingsPanelHeight, 400 };
		
		frame.setLayout(mainLayout);
		
		layout = new GridBagLayout();
		
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.NORTHWEST;
		c.insets = new Insets(0,0,2,0);
		
		layout.columnWidths = new int[] { 30, 100, 120, 70, 60, 60, 60 };
		layout.rowHeights = new int[cyclistNum + 2 + myTeam.size()];
		
		for (int i = 0; i < cyclistNum + 2 + myTeam.size(); i++)
			layout.rowHeights[i] = 25;
		
		
		standingPanel = new JPanel(layout);
		
		c.gridwidth = attributesNum;
		name = new JLabel("Standings");
		name.setOpaque(true);
		name.setFont(font2);
		name.setBackground(Color.green);
		standingPanel.add(name, c);
		
		c.gridwidth = 1;
		
		stats = new JLabel[attributesNum];
		
		for (int i = 0; i < attributesNum; i++) {
			c.gridx = i;
			c.gridy = 1;
			stats[i] = new JLabel(attributeNames[i]);
			stats[i].setOpaque(true);
			stats[i].setBackground(Color.yellow);
			stats[i].setFont(font);
			standingPanel.add(stats[i], c);
		}
		
		riders = new JLabel[cyclistNum + myTeam.size()][attributesNum];
		
		for (int i = 0; i < cyclistNum + myTeam.size(); i++) {
			for (int j = 0; j < attributesNum; j++) {
				c.gridx = j;
				c.gridy = i + 2;			
				
				if (j != 0)
					riders[i][j] = new JLabel();
				else
					riders[i][j] = new JLabel((i+1)+".");
				riders[i][j].setOpaque(true);
				riders[i][j].setBackground(Color.orange);
				riders[i][j].setFont(font);
				standingPanel.add(riders[i][j], c);
			}
		}	
		
		cb.gridx = 2;
		cb.gridy = 0;
		cb.gridheight = 1;
		frame.add(standingPanel, cb);
		standingPane = new JScrollPane(standingPanel);
		frame.add(standingPane, cb);
		
		
		userLayout = new GridBagLayout();
		
		GridBagConstraints cuser = new GridBagConstraints();
		cuser.fill = GridBagConstraints.BOTH;
		cuser.insets = new Insets(0,0,2,0);
		
		userLayout.columnWidths = new int[] { riderPanelWidth + 10 };
		userLayout.rowHeights = new int[10];
		
		for (int i = 0; i < 9; i++) 
				userLayout.rowHeights[i] = riderPanelHeight;

		
		userLayout.rowHeights[9] = 140;
		
		userPanel = new JPanel(userLayout);
		
		riderPanel = new RiderPanel[myTeam.size()];
	
		for (int i = 0 ; i < myTeam.size(); i++) {
			cuser.gridy = 8 - i;
			riderPanel[i] = new RiderPanel(riderPanelWidth, riderPanelHeight);
			riderPanel[i].addMouseListener(this);
			userPanel.add(riderPanel[i], cuser);
		}	
		
		cuser.gridy = 9;
		riderActions = new RiderActions(200, 140, this);
		userPanel.add(riderActions, cuser);
		
		cb.gridx = 0;
		cb.gridy = 0;
		cb.gridheight = 2;
		frame.add(userPanel, cb);
		setUserPanelNames();	
	
		GuiMenu menu = new GuiMenu(this);
		frame.setJMenuBar(menu);
	}


	public void setUserPanelNames() {		
		for (int i = 0; i < myTeam.size(); i++) {
			riderPanel[i].name = cyclists.get(myTeam.get(i).getCyclistId()).getSurname() + " "+cyclists.get(myTeam.get(i).getCyclistId()).getFirstName();
			riderPanel[i].repaint();
		}
	}
	
	
	private void userGUIrefresh() {	
		for (int i = 0; i < myTeam.size(); i++) {
			riderPanel[i].energy = (int)((double)riderPanel[i].width*0.8 * myTeam.get(i).getEnergy()/100.0);
			riderPanel[i].stamina = (int)((double)riderPanel[i].width*0.8 * myTeam.get(i).getStamina()/100.0);
			riderPanel[i].puls =(int)myTeam.get(i).getPuls();
			riderPanel[i].groupId = myTeam.get(i).getGroupId();
			riderPanel[i].power = myTeam.get(i).getPower();
			riderPanel[i].action = myTeam.get(i).getAction().charAt(0)+"";
			riderPanel[i].repaint();
		}	
		
		if (profileGui != null) {
			profileGui.x = (int)RaceSimulator.findGroupById(myTeam.get(0).getGroupId()).driven;
			profileGui.repaint();
		}
		
		double driven = RaceSimulator.groups.get(0).driven/50;
		double toGo = Race.getInstance().stage.length/50 - driven; 
		double speed = RaceSimulator.groups.get(0).speed;
		frame.setTitle("Cycling Manager - Race    Driven: "+df.format(driven)+" km    Finish: "+df.format(toGo)+"    speed: "+df.format(speed));
	}
	
	
	public void refresh(RaceSimulator simulator) {	
		int ptr = 0;	
		userGUIrefresh();
		
		int driv;
		driv = (int)RaceSimulator.groups.get(0).driven;
		
		for (int i = 0; i < RaceSimulator.groups.size(); i++) {

			for (int k = 0; k < RaceSimulator.groups.get(i).relayGroup.size(); k++) {
				Cyclist cyclist = cyclists.get(RaceSimulator.groups.get(i).relayGroup.get(k).getCyclistId());
				riders[ptr][1].setText(cyclist.getSurname());			
				riders[ptr][2].setText(cyclist.getTeamName());			
			//	riders[ptr][3].setText(((int)simulator.groups.get(i).relayGroup.get(k).getGroupId()+""));
				riders[ptr][3].setText(driv-(int)RaceSimulator.groups.get(i).driven+"");
				riders[ptr][4].setText((int)RaceSimulator.groups.get(i).relayGroup.get(k).getPuls()+"");
				riders[ptr][5].setText((int)RaceSimulator.groups.get(i).relayGroup.get(k).getStamina()+"");
				riders[ptr][6].setText((int)RaceSimulator.groups.get(i).relayGroup.get(k).getEnergy()+"");
				setColor(ptr, RaceSimulator.groups.get(i).relayGroup.get(k).getAction());
				ptr++;
			}
			
			for (int j = 0; j < RaceSimulator.groups.get(i).riders.size(); j++) {
				Cyclist cyclist = cyclists.get(RaceSimulator.groups.get(i).riders.get(j).getCyclistId());
				riders[ptr][1].setText(cyclist.getSurname());			
				riders[ptr][2].setText(cyclist.getTeamName());			
				//riders[ptr][3].setText(((int)simulator.groups.get(i).riders.get(j).getGroupId()+""));
				riders[ptr][3].setText(driv-(int)RaceSimulator.groups.get(i).driven+"");
				riders[ptr][4].setText((int)RaceSimulator.groups.get(i).riders.get(j).getPuls()+"");
				riders[ptr][5].setText((int)RaceSimulator.groups.get(i).riders.get(j).getStamina()+"");
				riders[ptr][6].setText((int)RaceSimulator.groups.get(i).riders.get(j).getEnergy()+"");
				setColor(ptr, RaceSimulator.groups.get(i).riders.get(j).getAction());
				ptr++;
			}
		}		
	}
	
	
	private void setColor(int nr, String action) {
		Color color;
		if (action.equals("Maintain Position")) 
			color = new Color(44, 255, 33);
		else if (action.equals("Relay"))
			color = new Color(33, 140, 255);
		else if (action.equals("Relay Queue"))
			color = new Color(33, 200, 255);
		else if (action.equals("Slow Down"))
			color = new Color(255, 0, 0);
		else
			color = new Color(255, 255, 0);
		
		for (int i = 0; i < 7; i++)
			riders[nr][i].setBackground(color);
	}
	
	/*
	private String setTimeLost(int timeLost) {
		String result = "";
		if (timeLost >= 60) 
			result += timeLost/60 + "\"";
		if (timeLost % 60 >= 10)
			result += (timeLost % 60)+"";
		else
			result += "0"+(timeLost % 60)+"";
		
		return result;		
	}
*/

	@Override
	public void mouseClicked(MouseEvent e) {
		for (int i = 0; i < riderPanel.length; i++) {
			if (e.getSource() == riderPanel[i]) {
				riderActions.setValue(myTeam.get(i).getPower());
				riderPanel[i].color = new Color(100,100,255);
				riderPanel[i].repaint();
				if (lastSelected != -1)
					riderPanel[lastSelected].color = new Color(160, 160, 160);
				lastSelected = i;
				break;
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {}

	@Override
	public void mouseExited(MouseEvent arg0) {}

	@Override
	public void mousePressed(MouseEvent arg0) {}

	@Override
	public void mouseReleased(MouseEvent arg0) {}
}
